import {
    uploadPicture
} from "../middleware/uploadPictureMiddleware";
import Comment from "../models/Comment";
import Post from "../models/Post";
import User from "../models/User";
import {
    fileRemover
} from "../utils/fileRemover";

const registerUser = async (req, res, next) => {
    try {
        const {
            name,
            email,
            password
        } = req.body;

        //查询是否存在此用户
        let user = await User.findOne({
            email
        });

        if (user) {
            throw new Error("用户已存在");
        }
        //创建一个用户
        user = await User.create({
            name,
            email,
            password
        });

        return res.status(201).json({
            _id: user._id,
            avatar: user.avatar,
            name: user.name,
            email: user.email,
            verified: user.verified,
            admin: user.admin,
            token: await user.generateJWT(),
        });
    } catch (error) {
        next(error);
    }
};

const loginUser = async (req, res, next) => {
    try {
        const {
            email,
            password
        } = req.body;

        let user = await User.findOne({
            email
        });

        if (!user) {
            throw new Error("未找到用户");
        }

        if (await user.comparePassword(password)) {
            return res.status(201).json({
                _id: user._id,
                avatar: user.avatar,
                name: user.name,
                email: user.email,
                verified: user.verified,
                admin: user.admin,
                token: await user.generateJWT(),
            });
        } else {
            throw new Error("密码或邮箱错误");
        }
    } catch (error) {
        next(error);
    }
};

const userProfile = async (req, res, next) => {
    try {
        let user = await User.findById(req.user._id);

        if (user) {
            return res.status(201).json({
                _id: user._id,
                avatar: user.avatar,
                name: user.name,
                email: user.email,
                verified: user.verified,
                admin: user.admin,
            });
        } else {
            let error = new Error("用户不存在");
            error.statusCode = 404;
            next(error);
        }
    } catch (error) {
        next(error);
    }
}

const updateProfile = async (req, res, next) => {
    try {
        const userIdToUpdate = req.params.userId;

        let userId = req.user._id;

        if(!req.user.admin && userIdToUpdate !== userId) {
            let error = new Error("没有权限");
            error.statusCode = 403;
            throw error;
        }

        let user = await User.findById(userIdToUpdate);

        if (!user) {
            throw new Error("用户不存在");
        }

        if(typeof req.body.admin !== "undefined" && req.user.admin) {
            user.admin = req.body.admin;
        }

        user.name = req.body.name || user.name;
        user.email = req.body.email || user.email;

        if (req.body.password && req.body.password.length < 6) {
            throw new Error("密码长度不能小于6位");
        } else if (req.body.password) {
            user.password = req.body.password;
        }

        const updatedUserProfile = await user.save();

        res.json({
            _id: updatedUserProfile._id,
            avatar: updatedUserProfile.avatar,
            name: updatedUserProfile.name,
            email: updatedUserProfile.email,
            verified: updatedUserProfile.verified,
            admin: updatedUserProfile.admin,
            token: await updatedUserProfile.generateJWT(),
        })

    } catch (error) {
        next(error);
    }
}

const updateProfilePicture = async (req, res, next) => {
    try {
        const upload = uploadPicture.single("profilePicture");
        upload(req, res, async function (err) {
            if (err) {
                const error = new Error("上传文件时存在错误" + err.message);
                next(error);
            } else {
                if (req.file) {
                    let filename;
                    let updatedUser = await User.findByIdAndUpdate(req.user._id);
                    filename = updatedUser.avatar;
                    if (filename) {
                        fileRemover(filename);
                    }
                    updatedUser.avatar = req.file.filename;
                    await updatedUser.save();
                    res.json({
                        _id: updatedUser._id,
                        avatar: updatedUser.avatar,
                        name: updatedUser.name,
                        email: updatedUser.email,
                        verified: updatedUser.verified,
                        admin: updatedUser.admin,
                        token: await updatedUser.generateJWT(),
                    })
                } else {
                    let filename;
                    let updatedUser = await User.findById(req.user._id);
                    filename = updatedUser.avatar;
                    updatedUser.avatar = "";
                    await updatedUser.save();
                    fileRemover(filename);
                    res.json({
                        _id: updatedUser._id,
                        avatar: updatedUser.avatar,
                        name: updatedUser.name,
                        email: updatedUser.email,
                        verified: updatedUser.verified,
                        admin: updatedUser.admin,
                        token: await updatedUser.generateJWT(),
                    })
                }
            }
        })
    } catch (error) {
        next(error);
    }
}

const getAllUsers = async (req, res, next) => {
    try {
        const filter = req.query.searchKeyword;
        let where = {}
        if (filter) {
            where.email = {
                $regex: filter,
                $options: "i"
            };
        }
        let query = User.find(where);
        const page = parseInt(req.query.page) || 1;
        const pageSize = parseInt(req.query.limit) || 10;
        const skip = (page - 1) * pageSize;
        const total = await User.find(where).countDocuments();
        const pages = Math.ceil(total / pageSize);

        res.header({
            'x-totalcount': JSON.stringify(total),
            'x-currentpage': JSON.stringify(page),
            'x-pagesize': JSON.stringify(pageSize),
            'x-totalpagecount': JSON.stringify(pages),
        })

        if (page > pages) {
            return res.json([])
        }

        const result = await query
            .skip(skip)
            .limit(pageSize)
            .sort({
                updatedAt: 'desc'
            });

        return res.json(result)
    } catch (error) {
        next(error);
    }
}

const deleteUser = async (req, res, next) => {
    try {
        let user = await User.findById(req.params.userId);
        if (!user) {
            throw new Error("用户未找到");
        }

        const postsToDelete = await Post.find({
            user: user._id
        });
        const postIdsToDelete = postsToDelete.map((post) => post._id);

        await Comment.deleteMany({
            post: {
                $in: postIdsToDelete
            }
        });

        await Post.deleteMany({
            _id: {
                $in: postIdsToDelete
            }
        })

        postIdsToDelete.forEach((post) => {
            fileRemover(post.photo);
        })

        await user.remove();
        fileRemover(user.avatar)
        res.status(204).json({
            message: "用户已删除"
        })
    } catch (error) {
        next(error)
    }
}

export {
    registerUser,
    loginUser,
    userProfile,
    updateProfile,
    updateProfilePicture,
    getAllUsers,
    deleteUser,
}